import MySQLdb
import requests
import json
import urllib.parse

headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36',
        'cookie': '_ga=GA1.3.1862801744.1582273520; search_history_yw_a=author%3D%26author_type1%3D0%26author_type2%3D1%26title%3D%25E5%2585%25B3%25E4%25BA%258E%25E6%2595%25B0%25E5%25AD%2597%25E5%259B%25BE%25E4%25B9%25A6%25E9%25A6%2586%25E7%259A%2584%25E5%2587%25A0%25E7%2582%25B9%25E8%25AE%25A4%25E8%25AF%2586%26title_type1%3D1%26ywqk%3D%25E6%2583%2585%25E6%258A%25A5%25E8%25B5%2584%25E6%2596%2599%25E5%25B7%25A5%25E4%25BD%259C%26ywqk_type1%3D1%26ywxj%3D%26ywnd1%3D1998%2C1999%2C2000%2C2001%2C2002%2C2003%2C2004%2C2005%2C2006%2C2007%2C2008%2C2009%2C2010%2C2011%2C2012%2C2013%2C2014%2C2015%2C2016%2C2017%2C2018%2C2019%2C2020%2C2021%26ywlx%3D%u8BF7%u9009%u62E9%26ywnd%3D%26search_model%3DAND%26order_type%3Dnum%26order_px%3DDESC%26pagesize%3D20; PHPSESSID=jnvq0ckefo06nfnh7c92affta4'
    }

def search_yw_id(title,journal):
    url = 'http://cssci.nju.edu.cn/control/controllers.php'
    values = {}
    a = title
    values['title'] = a
    journal_dic = {}
    journal_dic['journal'] = journal
    aa = str(urllib.parse.urlencode(values)).strip('title=')
    ywqk = str(urllib.parse.urlencode(journal_dic).strip('journal='))
    years = {}
    years['years'] = '1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021'
    yy = str(urllib.parse.urlencode(years)).strip('years=')
    params = {
        'control': 'search_base',
        'action': 'search_ywsy',
        'author': '',
        'author_type1': '1',
        'author_type2': '1',
        'title': aa,
        'title_type1': '1',
        'ywqk': ywqk,
        'ywqk_type1': '1',
        'ywxj': '',
        'ywnd': '',
        'ywnd1': yy,
        'ywnd2': '',
        'ywlx': '',
        'search_model': 'AND',
        'order_type': 'num',
        'order_px': 'DESC',
        'pagesize': '20',
        'search_tag': '0',
        'session_key': '837',
        'pagenow':'1'
    }
    res = requests.get(url, params=params, headers=headers)
    # print(res.url)
    res_json = res.json()
    if res_json['contents']:
        id = res_json['contents'][0]['id']
        znum = res_json['znum']
    else:
        id = ''
        znum = ''
    return id, znum

#根据id获取引证文献详细数据
def search_details_citations(id):
    url = 'http://cssci.nju.edu.cn/control/controllers.php'
    years = {}
    years['years'] = '1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021'
    yy = str(urllib.parse.urlencode(years)).strip('years=')
    params = {
        'control': 'search',
        'action': 'catation_id',
        'id':id,
        'ywnd':yy,
        'pagenow':'1'
    }
    res = requests.get(url, params=params, headers=headers)
    # print(res.url)
    res_json = res.json()
    return res_json['contents_list']

def main():
    #先从mysql中读取文章的篇名与期刊名
    conn = MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='root',
        db ='journal',
        use_unicode=True,
        charset='utf8'
        )
    cur = conn.cursor()
    aa = cur.execute('SELECT sno,lypm,qkmc from journal;')
    info = cur.fetchmany(aa)
    for li in info[70635:]:
        try:
            sno = li[0]
            title = li[1]
            journal = li[2]
            #通过一个函数获取引证文献查询的文章id号
            print(journal)
            print(title)
            id = search_yw_id(title,journal)[0]
            yw_lis = search_details_citations(id)
            znum = search_yw_id(title,journal)[1]
            if yw_lis:
                for j in yw_lis:
                    id_yw = j['id']
                    sno_yw = j['sno']
                    lypm_yw = j['lypm']
                    lypmp_yw = j['lypmp']
                    blpm_yw = j['blpm']
                    authors_yw = j['authors']
                    authors_jg_yw = j['authors_jg']
                    authors_address_yw = j['authors_address']
                    wzlx = j['wzlx']
                    qkno = j['qkno']
                    xkdm1 = j['xkdm1']
                    xkdm2 = j['xkdm2']
                    ym = j['ym']
                    ywsl = j['ywsl']
                    byc = j['byc']
                    xmlb = j['xmlb']
                    nian = j['nian']
                    juan = j['juan']
                    qkmc = j['qkmc']
                    sql = '''insert into `yzwx` (sno,lymp,id,znum,id_yw,sno_yw,lypm_yw,lypmp_yw,blpm_yw,authors_yw,authors_jg_yw,authors_address_yw,wzlx,qkno,xkdm1,xkdm2,ym,ywsl,byc,xmlb,nian,juan,qkmc) values ("''' + sno + '''","''' + title + '''","''' + id + '''","''' + znum + '''","''' + id_yw + '''","''' + sno_yw + '''","''' + lypm_yw + '''","''' + lypmp_yw + '''","''' + blpm_yw + '''","''' + authors_yw + '''","''' + authors_jg_yw + '''","''' + authors_address_yw + '''","''' + wzlx + '''","''' + qkno + '''","''' + xkdm1 + '''","''' + xkdm2 + '''","''' + ym + '''","''' + ywsl + '''","''' + byc + '''","''' + xmlb + '''","''' + nian + '''","''' + juan + '''","''' + qkmc + '''");'''
                    print(sql)
                    try:
                        cur.execute(sql)
                        conn.commit()
                    except Exception as e:
                        continue
        except Exception as e:
            continue
    cur.close()

if __name__ == '__main__':
    main()